package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.iwomy.secureplat.platform.constant.NotificationRecordConstants;
import com.iwomy.secureplat.platform.entity.BasicNotificationRecordEntity;
import com.iwomy.secureplat.platform.service.BasicNotificationRecordService;
import com.iwomy.secureplat.platform.service.MpService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 微信业务
 *
 * @author pig
 * @date 2023-08-25 14:56:47
 */
@Service
@Slf4j
public class MpServiceImpl implements MpService {
	@Resource
	private WxMpService mpService;

	@Resource
	private BasicNotificationRecordService basicNotificationRecordService;


	@Override
	public String sendTemplateMsg(String wxOpenId, String receiverName, String templateId, Map<String, String> data, String url) throws WxErrorException {
		if (ObjectUtil.isEmpty(wxOpenId)) {
			log.error("wxOpenId is null");
			return "未获取到openId";
		}
		WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
				.toUser(wxOpenId)
				.templateId(templateId)
				.url(url)
				.build();
		data.entrySet().forEach(x -> {
			templateMessage.addData(new WxMpTemplateData(x.getKey(), x.getValue(), "#000000"));
		});

		String s = mpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
		log.info("{}",s);
		return s;
	}
}